var express = require('express')
  , user = require('./routes/user')
  , http = require('http')
  , fs = require('fs')
  , routes = require('./routes')
  , ejs = require ('ejs')
  , util = require('util')
  , path = require('path')
  , config = require('./config');


var dbconn = require('mongoskin').db(config.host+':'+config.port+'/'+config.schema);

var app = express();

app.configure(function(){
  app.set('port', process.env.PORT || 3000);
  app.engine('html', require('ejs').__express);
  app.set('views', __dirname + '/views');
  app.set('view engine', 'html');
  app.use(express.favicon());
  app.use(express.logger('dev'));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(path.join(__dirname, 'public')));
});

app.configure('development', function(){
  app.use(express.errorHandler());
});

app.get('/',function(req, res){
  res.render('layout');
});

  app.get('/seecontacts', function(req, res, next) {
    dbConn.collection('contact').find().toArray(function(err, result) {
        if (err) throw err;
        console.log('Result: ' + util.inspect(result));
        res.send(result);
    });
  });

app.post('/contact',function(req, res){
  console.log('Body del req: ' + util.inspect(req.body));
  dbconn.collection('contact').insert(req.body);
  res.send(200);
})

http.createServer(app).listen(app.get('port'), function(){
  console.log("Express server listening on port " + app.get('port'));
});
//makeing some changes for  mercuril exercises purposes